package com.yangjian.web;

import com.yangjian.bean.ShoppingCart;
import com.yangjian.comon.R;
import com.yangjian.page.PageParameter;
import com.yangjian.service.ShoppingCartService;
import com.yangjian.util.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController {


    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        ShoppingCart shoppingCart1 = shoppingCartService.add(shoppingCart);
        return R.success(shoppingCart1);
    }


    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public R findShopping() {

        //调用service，把用户id放进去
        List<ShoppingCart> shoppingCarts = shoppingCartService.findByUserUd(BaseContext.getCurrentId());
        if (shoppingCarts != null) {
            //响应页面
            return R.success(shoppingCarts);
        } else {
            return R.error("查询购物车失败");
        }
    }

    /**
     * 修改购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R update(@RequestBody ShoppingCart shoppingCart) {
        int row = shoppingCartService.update(shoppingCart);

        if (row>0) {
            return R.success("修改成功");
        }

        return R.error("修改失败");
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public  R delete(){
        int row = shoppingCartService.delete(BaseContext.getCurrentId());
        if (row > 0) {
            return R.success("清空购物车成功~!");
        }
        return R.error("清空购物车失败~！");
    }



}
